<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Time 时间</title>
    <style>
        *{
            padding: 0;
            margin: 0;
        }
        .head-nav{
            display: flex;
            justify-content: space-between;
            padding: 10px;
            border-bottom: 1px solid #EEF0F4;
        }

        .list{
            margin: 20px;
        }
        .list ul li{
            list-style: none;
            height: 38px;
            line-height: 38px;
            background: #F7F8FA;
            margin: 10px 0;
            padding: 0px 10px;
            border-radius: 20px;
            font-weight: bold;
            font-size: 14px;
        }
        .list ul li:hover{
            background: #EEF0F4
        }
    </style>
</head>
<body>
    <div class="head-nav">
        <div onclick="history.go(-1)">《</div>
        <div>Time 时间</div>
        <div></div>
    </div>

    <div class="list">
        <ul>
            <li id="Countdown0" data-timer="1632912949000"></li>
            <li id="Countdown1" data-timer="1632922949000"></li>
            <li id="Countdown2" data-timer="1632932949000"></li>
            <li id="Countdown3" data-timer="1632942949000"></li>
            <li id="Countdown4" data-timer="1632952949000"></li>
        </ul>
    </div>
    <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
    <script>
        let id = [1,2,3,4,5]
        for(let i = 0; i<id.length; i++){
            TimeDown(`Countdown${i}`,$(`#Countdown${i}`).attr("data-timer"))
        }

        function TimeDown(id,endDateStr) {
            //结束时间
            // var endDate = new Date(endDateStr); // 传入标准时间格式
            var endDate = endDateStr; // 传入时间戳
            //当前时间
            var nowDate = new Date();
            //相差的总秒数
            var totalSeconds = parseInt((endDate - nowDate) / 1000);
            //天数
            var days = Math.floor(totalSeconds / (60 * 60 * 24)) <= 0 ? "00": Math.floor(totalSeconds / (60 * 60 * 24));
            //取模（余数）
            var modulo = totalSeconds % (60 * 60 * 24) <= 0 ? "00": totalSeconds % (60 * 60 * 24);
            //小时数
            var hours = Math.floor(modulo / (60 * 60)) <= 0 ? "00": Math.floor(modulo / (60 * 60));
            modulo = modulo % (60 * 60);
            //分钟
            var minutes = Math.floor(modulo / 60) <= 0 ? "00":  Math.floor(modulo / 60);
            //秒
            var seconds = (modulo % 60 <= 0) ? "00": (modulo % 60);

            //输出到页面
            document.getElementById(id).innerHTML = "剩余抢购:" + days + "天" + hours + "：" + minutes + "：" + seconds;

            //延迟一秒执行自己
            setTimeout(function () {
                TimeDown(id, endDateStr);
            }, 1000)
        }










        


        // 时间戳转换为标准格式
        function formatDate(datetime) {
            var date = new Date(datetime); //datetime时间戳为13位毫秒级别,如为10位需乘1000
            var month = ("0" + (date.getMonth() + 1)).slice(-2),
                sdate = ("0" + date.getDate()).slice(-2),
                hour = ("0" + date.getHours()).slice(-2),
                minute = ("0" + date.getMinutes()).slice(-2),
                second = ("0" + date.getSeconds()).slice(-2);
            var thatDate = date.getFullYear() + "-" + month + "-" + sdate + " " + hour + ":" + minute + ":" + second;
            // 返回
            return thatDate;
        }
        console.log("时间戳转换为标准格式:",formatDate(1632912949000)) //2020-09-03 06:24:07

        // 当前时间转换为标准格式
        function getFormatDate() {
            var date = new Date();
            var month = date.getMonth() + 1;
            var day = date.getDate();
            var hour = date.getHours();
            var minutes = date.getMinutes();
            var seconds = date.getSeconds();
            month = (month<10)? '0'+ month : month;
            day = (day<10)? '0'+ day : day;
            hour = (hour<10)? '0'+ hour : hour;
            minutes = (minutes<10)? '0'+ minutes : minutes;
            seconds = (seconds<10)? '0'+ seconds : seconds;
            var currentDate = date.getFullYear() + "-" + month + "-" + day
                    + " " + hour + ":" + minutes + ":" + seconds;
            return currentDate;
        }
        console.log(getFormatDate()) //2020-08-10 10:00:34


        // 将标准时间格式转化为时间戳
        function getTimeStamp(str){
            var date = new Date(str);
            // 可以准确精确到毫秒
            var time1 = date.getTime(date);
            // 可以准确精确到毫秒
            var time2 = date.valueOf(date);
            // 只能精确到秒（毫秒使用0000代替）
            var time3 = Date.parse(date);
            return time3
        }
        console.log("将标准时间格式转化为时间戳:",getTimeStamp('2021-09-29 18:55:49'))



        
    </script>
</body>
</html>